package com.jl.basicframework.utils.photoUtils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;

import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/***
 * @author 作者:姜澜
 * 时间:2022/2/17
 * 邮箱:316879104@pp.com
 * 类简述:相册中选择图片工具
 */
public class SelectPhotoUtils implements OnResultCallbackListener {

    private Activity mActivity;

    /**
     * 图片选择工具
     */
    private PictureSelectionModel pictureSelection;

    /**
     * 是否选择多张图片
     */
    private boolean isSelectionMultiImage;

    /**
     * 选择单张图片回调
     */
    private OnSelectionSingleImageResult singleImageResult;

    /**
     * 选择多张图片回调
     */
    private OnSelectionMultiImageResult multiImageResult;

    public SelectPhotoUtils(Context context) {
        this.mActivity = (Activity) context;
        initSelectionModel();
    }

    public SelectPhotoUtils(Activity mActivity) {
        this.mActivity = mActivity;
        initSelectionModel();
    }

    /**
     * 启动工具
     */
    public void startUtils(){
        setSelectionUtils();
    }

    /**
     * 初始化选择工具
     */
    private void initSelectionModel(){
        pictureSelection = PictureSelector
                .create(mActivity)
                //打开相册
                .openGallery(PictureMimeType.ofImage());

    }

    /**
     * 设置选择工具基本参数
     */
    private void setSelectionUtils(){
        //选择的类型 图片
        pictureSelection.isSingleDirectReturn(true)
                .loadImageEngine(GlideEngine.createGlideEngine())
                //大于400k进行压缩
                .compress(true)
                .minimumCompressSize(400);

        pictureSelection.forResult(PictureConfig.CHOOSE_REQUEST,this);
    }

    /**
     * 裁剪为圆形图片
     */
    public SelectPhotoUtils cropToRoundImage(){
        pictureSelection
                .enableCrop(true)
                .circleDimmedLayer(true)
                .showCropFrame(false)
                .showCropFrame(false)
                .setCircleDimmedBorderColor(Color.RED)
                .isSingleDirectReturn(true)
                .withAspectRatio(500,500);

        return this;
    }

    /**
     * 是否选择多张图片
     */
    private void isSelectionMultiImage(boolean b){
        isSelectionMultiImage = b;
    }

    /**
     * 设置文件回调
     */
    public SelectPhotoUtils setSelectionImageCall(OnSelectionSingleImageResult imageResult){
        pictureSelection.selectionMode(PictureConfig.SINGLE);
        isSelectionMultiImage(false);
        this.singleImageResult = imageResult;
        return this;
    }

    /**
     * 设置文件回调
     */
    public SelectPhotoUtils setSelectionImageCall(OnSelectionMultiImageResult imageResult){
        pictureSelection.selectionMode(PictureConfig.MULTIPLE);
        isSelectionMultiImage(true);
        this.multiImageResult = imageResult;
        return this;
    }


    @Override
    public void onResult(List<LocalMedia> result) {

        //检查图片列表长度
        if (0!=result.size()){

            //检查选择模式
            if (isSelectionMultiImage){
                //选择多张图片
                List<File> imageFileList = new ArrayList<>();
                for (LocalMedia localMedia : result) {
                    File imageFile = new File(localMedia.getCompressPath());
                    imageFileList.add(imageFile);
                }
                multiImageResult.onImageFileList(imageFileList);

            }else {
                //选择单张图片
                File imageFile = new File(result.get(0).getCompressPath());
                singleImageResult.onImageFile(imageFile);
            }
        }
    }

    @Override
    public void onCancel() {

        if (null!=singleImageResult){
            singleImageResult.onError("未选择图片");
        }

        if (null!=multiImageResult){
            multiImageResult.onError("未选择图片");
        }

    }

    /**
     * 选择单张图片回调接口
     */
    public interface OnSelectionSingleImageResult{

        /**
         * 图片文件回调
         * @param imageFile
         */
        void onImageFile(File imageFile);

        /**
         * 错误回调
         * @param errorMsg
         */
        void onError(String errorMsg);
    }

    /**
     * 选择多张图片回调接口
     */
    public interface OnSelectionMultiImageResult{
        /**
         * 图片文件列表回调
         * @param imageFileList
         */
        void onImageFileList(List<File> imageFileList);

        /**
         * 错误回调
         * @param errorMsg
         */
        void onError(String errorMsg);
    }
}
